<template>
  <div class="login-container">
    <el-row>
      <el-col>
        <el-form ref="ruleFormRef"
                 class="login_form"
                 :model="loginForm"
                 :rules="rule">
          <h1>Hello</h1>
          <h2>欢迎登录DaDa应用</h2>
          <el-form-item prop="username">
            <el-input :prefix-icon="User"
                      v-model="loginForm.username"
                      type="text"
                      placeholder="账号"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input :prefix-icon="Lock"
                      v-model="loginForm.password"
                      type="password"
                      show-password
                      placeholder="密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button class="login-button"
                       type="primary"
                       @click="login(ruleFormRef)"
                       :loading="loading">登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import {User, Lock} from '@element-plus/icons-vue'
import {reactive, ref} from "vue";
import useUserStore from '@/store/modules/user';
//@ts-ignore
import {useRouter} from "vue-router";
import {ElNotification} from "element-plus";


let loginForm = reactive({username: 'admin', password: 'atguigu123'})
let userStore = useUserStore();
let loading = ref(false);
let $router = useRouter();
let ruleFormRef = ref();


function dateString() {
  let hour = new Date().getHours();
  if (hour >= 6 && hour < 12) {
    return '上午好！'
  } else if (hour >= 12 && hour < 18) {
    return '下午好！'
  } else {
    return '晚上好！'
  }
}

const login = async (ruleFormRef: any) => {
  await ruleFormRef.value.validate();
  loading.value = true;
  try {
    await userStore.userLogin(loginForm);
    await $router.push('/');
    ElNotification({
      title: 'Hi,' + dateString(),
      message: '欢迎回来',
      type: 'success',
    })
    loading.value = false;
  } catch (error) {
    loading.value = false;
    ElNotification({
      message: error.message,
      type: 'error',
    })
  }
}

const validateUserName = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入用户名'))
  } else {
    if (value.length >= 5 && value.length <= 10) {
      callback()
    } else {
      callback(new Error('账号长度五至十位！'))
    }
  }
}


const rule = ({
  username: [
    {validator: validateUserName, trigger: 'change'}
    // {required: true, message: '请输入账号', trigger: 'blur'},
    // {min: 3, max: 8, message: '账号长度在 3 到 8 个字符', trigger: 'blur'},
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 6, max: 10, message: '密码长度在 6 到 10 个字符'}
  ]
})


</script>

<style scoped lang="scss">
.login-container {
  width: 100%;
  height: 100vh;
  background: url("@/assets/preview.png") no-repeat;
  background-size: cover;
}

.login_form {
  top: 30vh;
  position: relative;
  width: 360px;
  margin: 0 auto;
  background: white;
  padding: 28px;
  border-radius: 10px;
}

.h1 {
  color: white;
  font-size: 40px;
}

.h2 {
  color: white;
  font-size: 20px;
  margin: 20px 0px;
}

.login-button {
  width: 100%;
}

</style>